package quq.missq.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import quq.missq.BaseFragmentActivity;
import quq.missq.MLog;
import quq.missq.R;
import quq.missq.adapter.ListFragmentAdapter;
import quq.missq.fragments.CollectionBigShootFragment;
import quq.missq.fragments.CollectionTopicFragment;
import quq.missq.utils.UserTools;


public class MyCollectionActivity extends BaseFragmentActivity implements View.OnClickListener{

    private ViewPager mViewPager;
    private ListFragmentAdapter adapter;
    private ArrayList<Fragment> fragments;
    private TextView mBigShoot;
    private TextView mTopic;
    private ArrayList<TextView> tabTexts;

    private ImageView mBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_collection_activity);
        initView();
        initData();
        initListener();
        if(UserTools.getUser(this) != null){
            MLog.e("token",UserTools.getUser(this).getAcc_token());
        }

    }

    private void initView(){
        mViewPager = (ViewPager)findViewById(R.id.viewPager);
        mBigShoot = (TextView)findViewById(R.id.bigShoot);
        mTopic = (TextView)findViewById(R.id.topic);
        mBack = (ImageView)findViewById(R.id.back);
    }

    private void initData(){
        tabTexts = new ArrayList<>();
        tabTexts.add(mBigShoot);
        tabTexts.add(mTopic);
        adapter = new ListFragmentAdapter(getSupportFragmentManager());
        fragments = new ArrayList<>();
        fragments.add(new CollectionBigShootFragment());
        fragments.add(new CollectionTopicFragment());
        adapter.setData(fragments);
        mViewPager.setAdapter(adapter);
        mViewPager.setCurrentItem(0);
        changeTabTextStyle(0);
    }

    private void initListener(){
        mBack.setOnClickListener(this);
        mBigShoot.setOnClickListener(this);
        mTopic.setOnClickListener(this);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPx) {
            }

            @Override
            public void onPageSelected(int i) {
                changeTabTextStyle(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bigShoot:
                mViewPager.setCurrentItem(0);
                changeTabTextStyle(0);
                break;
            case R.id.topic:
                mViewPager.setCurrentItem(1);
                changeTabTextStyle(1);
                break;
            case R.id.back:
                finish();
                break;
            default:break;
        }
    }

    private void changeTabTextStyle(int position){
        TextView curTv = tabTexts.get(position);
        curTv.setTextSize(15);
        curTv.setTextColor(getResources().getColor(android.R.color.black));
        TextView otherTv;
        for(int i=0; i<tabTexts.size();i++){
            if(i != position){
                otherTv = tabTexts.get(i);
                otherTv.setTextSize(14);
                otherTv.setTextColor(getResources().getColor(android.R.color.darker_gray));
            }
        }
    }

}
